package com.yixun.qfbao.response.premises.detail;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yixun.qfbao.apidto.PremisesDetailResDto;
import com.yixun.qfbao.model.enums.TimeUnitTypeEnum;
import com.yixun.qfbao.xf.model.enums.ReportWayEnum;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author zilong.liu
 * @date 2020-8-12
 */
@Data
public class RuleInfo implements Serializable{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RuleInfo.class);
	
	public RuleInfo() {
	}

	public RuleInfo(PremisesDetailResDto detailResDto) {
		String realReportPhone = ReportWayEnum.getDescByNo(detailResDto.getReportWay());
		if(StringUtils.isEmpty(realReportPhone)) {
			this.reportPhone = "不限";
		}else {
			this.reportPhone = realReportPhone;
			if("隐号".equals(this.reportPhone)) {
				this.reportPhone = "前3后4";
			}
		}
//		this.specialTime = getTimeInfo(detailResDto.getSpecialTime(), detailResDto.getSpecialTimeType());
		LOGGER.info("开始组装ruleInfo信息");
		String specialTimeType = detailResDto.getSpecialTimeType();
		String specialTimeInfo = getTimeInfo(detailResDto.getSpecialTime(), specialTimeType);
		LOGGER.info("detailResDto.getSpecialTime():{}", detailResDto.getSpecialTime());
		LOGGER.info("specialTimeType:{}", specialTimeType);
		if(StringUtils.isNotEmpty(detailResDto.getSpecialTime()) &&
				(TimeUnitTypeEnum.DAY.getNo().equals(specialTimeType) ||
						TimeUnitTypeEnum.HOUR.getNo().equals(specialTimeType) ||
						TimeUnitTypeEnum.MINUT.getNo().equals(specialTimeType))) {
			this.specialTime = specialTimeInfo;
		} else if("不需要".equals(specialTimeInfo)){
			this.specialTime = "无限制";
		} else if(StringUtils.isEmpty(specialTimeInfo)){
			this.specialTime = "无限制";
		} else {
			this.specialTime = specialTimeInfo;
		} 
		try {
			String address = InetAddress.getLocalHost().getHostAddress();
			LOGGER.info("this is the address:{}", address);
		} catch (UnknownHostException e) {
			LOGGER.info("获取IP异常");
		}
		LOGGER.info("this is specialTime:{}", specialTime);
		String reportProtect = String.valueOf(detailResDto.getReportProtect());
		String reportProtectType = String.valueOf(detailResDto.getReportProtectType());
		if((StringUtils.isEmpty(reportProtect) || "null".equals(reportProtect)) 
				&& (StringUtils.isEmpty(reportProtectType) || "null".equals(reportProtectType))) {
			this.reportProtectTime = "无保护";
		}else {
			this.reportProtectTime = getTimeInfo(reportProtect, reportProtectType);
			if("不需要".equals(reportProtectTime)) {
				this.reportProtectTime = "无保护";
			}
		}
		
		String reportValid = String.valueOf(detailResDto.getReportValid());
		String reportValidType = String.valueOf(detailResDto.getReportValidType());
		if((StringUtils.isEmpty(reportValid) || "null".equals(reportValid)) 
				&& (StringUtils.isEmpty(reportValidType) || "null".equals(reportValidType))) {
			this.reportValidTime = "无期限";
		}else {
			this.reportValidTime = getTimeInfo(String.valueOf(detailResDto.getReportValid()), 
					String.valueOf(detailResDto.getReportValidType()));
		}
		
		
		String visitBaohu = detailResDto.getVisitBaohu();
		String visitBaohuType = detailResDto.getVisitBaohuType();
		if((StringUtils.isEmpty(visitBaohu) ||  "null".equals(visitBaohu)) 
				&& (StringUtils.isEmpty(visitBaohuType) ||  "null".equals(visitBaohuType))) {
			this.visitProtectTime = "无期限";
		}else {
			this.visitProtectTime = getTimeInfo(visitBaohu, visitBaohuType);
			if("不需要".equals(this.visitProtectTime)) {
				this.visitProtectTime = "无保护";
			}
		}
		
		this.commisionWay = detailResDto.getCommissionWay();
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 对应数据库中的reportWay字段
	 */
	@ApiModelProperty(value = "报备规则-报备号码")
	private String reportPhone;
	
	/**
	 * 对应数据库special_time字段 + special_time_type
	 */
	@ApiModelProperty(value = "报备规则-报备时间")
	private String specialTime;
	
	@ApiModelProperty(value = "报备规则-报备保护时间")
	private String reportProtectTime;
	
	@ApiModelProperty(value = "报备规则-报备有效时间")
	private String reportValidTime;
	
	/**
	 * 对应数据库字段 visit_baohu
	 */
	@ApiModelProperty(value = "带看规则-到访保护时间")
	private String visitProtectTime;
	
	/**
	 * 取值对应枚举CommissionWayEnum
	 */
	@ApiModelProperty(value = "结佣规则-结佣规则")
	private String commisionWay;
	
	/**
	 * 根据时间值以及时间单位类型获取时间全拼
	 * @param time
	 * @param timeUnitType
	 * @return
	 */
	public String getTimeInfo(String time, String timeUnitType) {
		timeUnitType = StringUtils.defaultIfBlank(timeUnitType, TimeUnitTypeEnum.NONE.getNo());
		if("4".equals(timeUnitType) || "5".equals(timeUnitType)) {
			return TimeUnitTypeEnum.getDescByNo(timeUnitType);
		}
		String timeUnitDesc = TimeUnitTypeEnum.getDescByNo(timeUnitType) == null? "": TimeUnitTypeEnum.getDescByNo(timeUnitType);
		
		try {
			if("1".equals(timeUnitType)) {
				int timeInt = Integer.parseInt(time)/1440;
				time = String.valueOf(timeInt);
			}else if("2".equals(timeUnitType)) {
				int timeInt = Integer.parseInt(time)/60;
				time = String.valueOf(timeInt);
			}
		} catch (NumberFormatException e) {
			time = "";
		}
		if(time == null || timeUnitType == null || "null".equals(time) || "null".equals(timeUnitDesc) ) {
			return "";
		}
		return time + timeUnitDesc;
	}
}
